package com.ruoyi.bim.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.banboocloud.Codec.BamboocloudFacade;
import com.ruoyi.bim.entity.AttributeEntity;
import com.ruoyi.bim.utils.BamboocloudUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.system.dept.domain.Dept;
import com.ruoyi.project.system.dept.service.IDeptService;
import com.ruoyi.project.system.post.domain.Post;
import com.ruoyi.project.system.post.service.IPostService;
import com.ruoyi.project.system.role.domain.Role;
import com.ruoyi.project.system.role.service.IRoleService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.domain.UserForBim;
import com.ruoyi.project.system.user.service.IUserService;
import javafx.geometry.Pos;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("bimService")
public class BIMController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IDeptService deptService;
    @Autowired
    private IPostService postService;
    //    @RequestMapping("hello")
//    public String hello(@RequestHeader("userId") String userId) {
//        return userId;
//    }
    public Map<String, Object> checkRequest(HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("checkPass", false);
        resultMap.put("reqMap", "");
        resultMap.put("errorMsgStr", "");
        String requestBody = BamboocloudUtils.getRequestBody(request);
        //解密json字符传
        requestBody = BamboocloudUtils.getPlaintext(requestBody, "123456", "AES");
        Map<String, Object> reqMap = (Map<String, Object>) JSONObject.parse(requestBody);
        resultMap.put("reqMap", reqMap);
        boolean checkPass = false;
        if (BamboocloudUtils.verify(reqMap, "MD5").booleanValue()) {
            String username = (String) reqMap.get("bimRemoteUser");
            String password = (String) reqMap.get("bimRemotePwd");
            //判断接口中的调用名及调用密码是否正确
            if (BamboocloudUtils.checkUsernamePassword(username, password)) {
                checkPass = true;
            }
        }
        if (!checkPass) {
            String resultStr = "";
            JSONObject resultObj = new JSONObject();
            resultObj.put("bimRequestId", "");
            resultObj.put("resultCode", "505");
            resultObj.put("message", "连接失败,请检查连接器配置的参数");
            resultStr = JSONObject.toJSONString(resultObj);
            //返回加密的json字符串
            resultStr = BamboocloudFacade.encrypt(resultStr, "123456", "AES");
            resultMap.put("errorMsgStr", resultStr);
        }
        resultMap.put("checkPass", checkPass);
        return resultMap;
    }

    @RequestMapping("SchemaService")
    public String schema(HttpServletRequest request) {
        Map<String, Object> checkResult = checkRequest(request);
        boolean checkPass = (boolean)checkResult.get("checkPass");
        if(!checkPass){
            return Objects.toString(checkResult.get("errorMsgStr"));
        }

        Map<String, Object> schema = new HashMap<>();
        List<AttributeEntity> accountAttrList = new ArrayList<AttributeEntity>();
        AttributeEntity attr1 = new AttributeEntity();
        attr1.setType("Long");
        attr1.setName("userId");
        attr1.setRequired(false);
        attr1.setMultivalued(false);
        accountAttrList.add(attr1);
        AttributeEntity attr2 = new AttributeEntity();
        attr2.setType("Long");
        attr2.setName("deptId");
        attr2.setRequired(false);
        attr2.setMultivalued(false);
        accountAttrList.add(attr2);
        AttributeEntity attr3 = new AttributeEntity();
        attr3.setType("String");
        attr3.setName("loginName");
        attr3.setRequired(false);
        attr3.setMultivalued(false);
        accountAttrList.add(attr3);
        AttributeEntity attr4 = new AttributeEntity();
        attr4.setType("String");
        attr4.setName("userName");
        attr4.setRequired(false);
        attr4.setMultivalued(false);
        accountAttrList.add(attr4);
        AttributeEntity attr5 = new AttributeEntity();
        attr5.setType("String");
        attr5.setName("userType");
        attr5.setRequired(false);
        attr5.setMultivalued(false);
        accountAttrList.add(attr5);
        AttributeEntity attr6 = new AttributeEntity();
        attr6.setType("String");
        attr6.setName("email");
        attr6.setRequired(false);
        attr6.setMultivalued(false);
        accountAttrList.add(attr6);
        AttributeEntity attr7 = new AttributeEntity();
        attr7.setType("String");
        attr7.setName("phonenumber");
        attr7.setRequired(false);
        attr7.setMultivalued(false);
        accountAttrList.add(attr7);
        AttributeEntity attr8 = new AttributeEntity();
        attr8.setType("String");
        attr8.setName("sex");
        attr8.setRequired(false);
        attr8.setMultivalued(false);
        accountAttrList.add(attr8);
        AttributeEntity attr9 = new AttributeEntity();
        attr9.setType("String");
        attr9.setName("avatar");
        attr9.setRequired(false);
        attr9.setMultivalued(false);
        accountAttrList.add(attr9);
        AttributeEntity attr10 = new AttributeEntity();
        attr10.setType("String");
        attr10.setName("password");
        attr10.setRequired(false);
        attr10.setMultivalued(false);
        accountAttrList.add(attr10);
        AttributeEntity attr11 = new AttributeEntity();
        attr11.setType("String");
        attr11.setName("salt");
        attr11.setRequired(false);
        attr11.setMultivalued(false);
        accountAttrList.add(attr11);
        AttributeEntity attr12 = new AttributeEntity();
        attr12.setType("String");
        attr12.setName("status");
        attr12.setRequired(false);
        attr12.setMultivalued(false);
        accountAttrList.add(attr12);
        AttributeEntity attr13 = new AttributeEntity();
        attr13.setType("String");
        attr13.setName("delFlag");
        attr13.setRequired(false);
        attr13.setMultivalued(false);
        accountAttrList.add(attr13);
        AttributeEntity attr14 = new AttributeEntity();
        attr14.setType("String");
        attr14.setName("loginIp");
        attr14.setRequired(false);
        attr14.setMultivalued(false);
        accountAttrList.add(attr14);
        AttributeEntity attr15 = new AttributeEntity();
        attr15.setType("String");
        attr15.setName("loginDate");
        attr15.setRequired(false);
        attr15.setMultivalued(false);
        accountAttrList.add(attr15);
        AttributeEntity attr16 = new AttributeEntity();
        attr16.setType("String");
        attr16.setName("pwdUpdateDate");
        attr16.setRequired(false);
        attr16.setMultivalued(false);
        accountAttrList.add(attr16);
        AttributeEntity attr17 = new AttributeEntity();
        attr17.setType("String");
        attr17.setName("createBy");
        attr17.setRequired(false);
        attr17.setMultivalued(false);
        accountAttrList.add(attr17);
        AttributeEntity attr18 = new AttributeEntity();
        attr18.setType("String");
        attr18.setName("createTime");
        attr18.setRequired(false);
        attr18.setMultivalued(false);
        accountAttrList.add(attr18);
        AttributeEntity attr19 = new AttributeEntity();
        attr19.setType("String");
        attr19.setName("updateBy");
        attr19.setRequired(false);
        attr19.setMultivalued(false);
        accountAttrList.add(attr19);
        AttributeEntity attr20 = new AttributeEntity();
        attr20.setType("String");
        attr20.setName("updateTime");
        attr20.setRequired(false);
        attr20.setMultivalued(false);
        accountAttrList.add(attr20);
        AttributeEntity attr21 = new AttributeEntity();
        attr21.setType("String");
        attr21.setName("remark");
        attr21.setRequired(false);
        attr21.setMultivalued(false);
        accountAttrList.add(attr21);
        AttributeEntity attr22 = new AttributeEntity();
        attr22.setMultivalued(true);
        attr22.setName("roleIds");
        attr22.setRequired(false);
        attr22.setType("String");
        accountAttrList.add(attr22);
        AttributeEntity attr23 = new AttributeEntity();
        attr23.setMultivalued(true);
        attr23.setName("postIds");
        attr23.setRequired(false);
        attr23.setType("String");
        accountAttrList.add(attr23);

        List<AttributeEntity> organizationList = new ArrayList<AttributeEntity>();
        AttributeEntity org1 = new AttributeEntity();
        org1.setType("Long");
        org1.setName("deptId");
        org1.setRequired(false);
        org1.setMultivalued(false);
        organizationList.add(org1);
        AttributeEntity org2 = new AttributeEntity();
        org2.setType("Long");
        org2.setName("parentId");
        org2.setRequired(false);
        org2.setMultivalued(false);
        organizationList.add(org2);
        AttributeEntity org3 = new AttributeEntity();
        org3.setType("String");
        org3.setName("ancestors");
        org3.setRequired(false);
        org3.setMultivalued(false);
        organizationList.add(org3);
        AttributeEntity org4 = new AttributeEntity();
        org4.setType("String");
        org4.setName("deptName");
        org4.setRequired(false);
        org4.setMultivalued(false);
        organizationList.add(org4);
        AttributeEntity org5 = new AttributeEntity();
        org5.setType("Long");
        org5.setName("orderNum");
        org5.setRequired(false);
        org5.setMultivalued(false);
        organizationList.add(org5);
        AttributeEntity org6 = new AttributeEntity();
        org6.setType("String");
        org6.setName("leader");
        org6.setRequired(false);
        org6.setMultivalued(false);
        organizationList.add(org6);
        AttributeEntity org7 = new AttributeEntity();
        org7.setType("String");
        org7.setName("phone");
        org7.setRequired(false);
        org7.setMultivalued(false);
        organizationList.add(org7);
        AttributeEntity org8 = new AttributeEntity();
        org8.setType("String");
        org8.setName("email");
        org8.setRequired(false);
        org8.setMultivalued(false);
        organizationList.add(org8);
        AttributeEntity org9 = new AttributeEntity();
        org9.setType("String");
        org9.setName("status");
        org9.setRequired(false);
        org9.setMultivalued(false);
        organizationList.add(org9);
        AttributeEntity org10 = new AttributeEntity();
        org10.setType("String");
        org10.setName("delFlag");
        org10.setRequired(false);
        org10.setMultivalued(false);
        organizationList.add(org10);
        AttributeEntity org11 = new AttributeEntity();
        org11.setType("String");
        org11.setName("createBy");
        org11.setRequired(false);
        org11.setMultivalued(false);
        organizationList.add(org11);
        AttributeEntity org12 = new AttributeEntity();
        org12.setType("String");
        org12.setName("createTime");
        org12.setRequired(false);
        org12.setMultivalued(false);
        organizationList.add(org12);
        AttributeEntity org13 = new AttributeEntity();
        org13.setType("String");
        org13.setName("updateBy");
        org13.setRequired(false);
        org13.setMultivalued(false);
        organizationList.add(org13);
        AttributeEntity org14 = new AttributeEntity();
        org14.setType("String");
        org14.setName("updateTime");
        org14.setRequired(false);
        org14.setMultivalued(false);
        organizationList.add(org14);

        List<AttributeEntity> roleList = new ArrayList<AttributeEntity>();
        AttributeEntity role1 = new AttributeEntity();
        role1.setType("Long");
        role1.setName("roleId");
        role1.setRequired(false);
        role1.setMultivalued(false);
        roleList.add(role1);
        AttributeEntity role2 = new AttributeEntity();
        role2.setType("String");
        role2.setName("roleName");
        role2.setRequired(false);
        role2.setMultivalued(false);
        roleList.add(role2);
        AttributeEntity role3 = new AttributeEntity();
        role3.setType("String");
        role3.setName("roleKey");
        role3.setRequired(false);
        role3.setMultivalued(false);
        roleList.add(role3);
        AttributeEntity role4 = new AttributeEntity();
        role4.setType("Long");
        role4.setName("roleSort");
        role4.setRequired(false);
        role4.setMultivalued(false);
        roleList.add(role4);
        AttributeEntity role5 = new AttributeEntity();
        role5.setType("String");
        role5.setName("dataScope");
        role5.setRequired(false);
        role5.setMultivalued(false);
        roleList.add(role5);
        AttributeEntity role6 = new AttributeEntity();
        role6.setType("String");
        role6.setName("status");
        role6.setRequired(false);
        role6.setMultivalued(false);
        roleList.add(role6);
        AttributeEntity role7 = new AttributeEntity();
        role7.setType("String");
        role7.setName("delFlag");
        role7.setRequired(false);
        role7.setMultivalued(false);
        roleList.add(role7);
        AttributeEntity role8 = new AttributeEntity();
        role8.setType("String");
        role8.setName("createBy");
        role8.setRequired(false);
        role8.setMultivalued(false);
        roleList.add(role8);
        AttributeEntity role9 = new AttributeEntity();
        role9.setType("String");
        role9.setName("createTime");
        role9.setRequired(false);
        role9.setMultivalued(false);
        roleList.add(role9);
        AttributeEntity role10 = new AttributeEntity();
        role10.setType("String");
        role10.setName("updateBy");
        role10.setRequired(false);
        role10.setMultivalued(false);
        roleList.add(role10);
        AttributeEntity role11 = new AttributeEntity();
        role11.setType("String");
        role11.setName("updateTime");
        role11.setRequired(false);
        role11.setMultivalued(false);
        roleList.add(role11);
        AttributeEntity role12 = new AttributeEntity();
        role12.setType("String");
        role12.setName("remark");
        role12.setRequired(false);
        role12.setMultivalued(false);
        roleList.add(role12);

        List<AttributeEntity> postList = new ArrayList<AttributeEntity>();
        AttributeEntity post1 = new AttributeEntity();
        post1.setType("Integer");
        post1.setName("postId");
        post1.setRequired(false);
        post1.setMultivalued(false);
        postList.add(post1);
        AttributeEntity post2 = new AttributeEntity();
        post2.setType("Long");
        post2.setName("postCode");
        post2.setRequired(false);
        post2.setMultivalued(false);
        postList.add(post2);
        AttributeEntity post3 = new AttributeEntity();
        post3.setType("Long");
        post3.setName("postName");
        post3.setRequired(false);
        post3.setMultivalued(false);
        postList.add(post3);

        schema.put("account", accountAttrList);
        schema.put("organization", organizationList);
        schema.put("role", roleList);
        schema.put("post", postList);

        String mapJson = JSON.toJSONString(schema);
        mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
        return mapJson;
    }

    @RequestMapping("UserCreateService")
    public String userCreate(HttpServletRequest request) {
        Map<String, Object> checkResult = checkRequest(request);
        boolean checkPass = (boolean)checkResult.get("checkPass");
        if(!checkPass){
            return Objects.toString(checkResult.get("errorMsgStr"));
        }

        Map<String, Object> reqMap = (Map<String, Object>)checkResult.get("reqMap");

        List<Long> roleIds = new ArrayList<>();
        List<Long> postIds = new ArrayList<>();
        if (reqMap.get("roleIds") != null && reqMap.get("roleIds") instanceof String) {
            Long roleId = Long.valueOf(Objects.toString(reqMap.get("roleIds")));
            roleIds.add(roleId);
        }
        if (reqMap.get("roleIds") != null && reqMap.get("roleIds") instanceof ArrayList) {
            List roIds = (ArrayList) reqMap.get("roleIds");
            for (int i = 0; i < roIds.size(); i++) {
                roleIds.add(Long.valueOf(Objects.toString(roIds.get(i))));
            }
        }
        reqMap.remove("roleIds");

        if (reqMap.get("postIds") != null && reqMap.get("postIds") instanceof String) {
            Long postId = Long.valueOf(Objects.toString(reqMap.get("postIds")));
            postIds.add(postId);
        }
        if (reqMap.get("postIds") != null && reqMap.get("postIds") instanceof ArrayList) {
            List poIds = (ArrayList) reqMap.get("postIds");
            for (int i = 0; i < poIds.size(); i++) {
                postIds.add(Long.valueOf(Objects.toString(poIds.get(i))));
            }
        }
        reqMap.remove("postIds");

        String s = JSON.toJSONString(reqMap);
        User user = JSONObject.parseObject(s, User.class);

        Long[] roleIdsArray = roleIds.toArray(new Long[roleIds.size()]);
        Long[] postIdsArray = postIds.toArray(new Long[postIds.size()]);
        user.setRoleIds(roleIdsArray);
        user.setPostIds(postIdsArray);
        userService.insertUser(user);
        final Long uid = user.getUserId();
        Map<String, Object> schema = new HashMap<String, Object>();
        schema.put("uid", uid);
        schema.put("bimRequestId", reqMap.get("bimRequestId"));
        schema.put("resultCode", "0");
        schema.put("message", "success");
        String mapJson = JSON.toJSONString(schema);
        mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
        return mapJson;
    }

    @RequestMapping("UserDeleteService")
    public String userDelete(HttpServletRequest request) {
        Map<String, Object> checkResult = checkRequest(request);
        boolean checkPass = (boolean)checkResult.get("checkPass");
        if(!checkPass){
            return Objects.toString(checkResult.get("errorMsgStr"));
        }

        Map<String, Object> reqMap = (Map<String, Object>)checkResult.get("reqMap");

        userService.deleteUserByIdReal(Long.parseLong(Objects.toString(reqMap.get("userId"), "")));
        Map<String, Object> schema = new HashMap<String, Object>();
        schema.put("bimRequestId", reqMap.get("bimRequestId"));
        schema.put("resultCode", "0");
        schema.put("message", "success");
        String mapJson = JSON.toJSONString(schema);
        mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
        return mapJson;
    }

    @RequestMapping("UserUpdateService")
    public String userUpdate(HttpServletRequest request) {
        Map<String, Object> checkResult = checkRequest(request);
        boolean checkPass = (boolean)checkResult.get("checkPass");
        if(!checkPass){
            return Objects.toString(checkResult.get("errorMsgStr"));
        }

        Map<String, Object> reqMap = (Map<String, Object>)checkResult.get("reqMap");

        List<Long> roleIds = new ArrayList<>();
        List<Long> postIds = new ArrayList<>();
        if (reqMap.get("roleIds") != null && reqMap.get("roleIds") instanceof String) {
            Long roleId = Long.valueOf(Objects.toString(reqMap.get("roleIds")));
            roleIds.add(roleId);
        }
        if (reqMap.get("roleIds") != null && reqMap.get("roleIds") instanceof ArrayList) {
            List roIds = (ArrayList) reqMap.get("roleIds");
            for (int i = 0; i < roIds.size(); i++) {
                roleIds.add(Long.valueOf(Objects.toString(roIds.get(i))));
            }
        }
        reqMap.remove("roleIds");

        if (reqMap.get("postIds") != null && reqMap.get("postIds") instanceof String) {
            Long postId = Long.valueOf(Objects.toString(reqMap.get("postIds")));
            postIds.add(postId);
        }
        if (reqMap.get("postIds") != null && reqMap.get("postIds") instanceof ArrayList) {
            List poIds = (ArrayList) reqMap.get("postIds");
            for (int i = 0; i < poIds.size(); i++) {
                postIds.add(Long.valueOf(Objects.toString(poIds.get(i))));
            }
        }
        reqMap.remove("postIds");

        String s = JSON.toJSONString(reqMap);
        User user = JSONObject.parseObject(s, User.class);

        Long[] roleIdsArray = roleIds.toArray(new Long[roleIds.size()]);
        Long[] postIdsArray = postIds.toArray(new Long[postIds.size()]);
        user.setRoleIds(roleIdsArray);
        user.setPostIds(postIdsArray);
        Long userId = Long.parseLong(Objects.toString(reqMap.get("userId")));
        user.setUserId(userId);
        userService.updateUser(user);
        Map<String, Object> schema = new HashMap<String, Object>();
        schema.put("bimRequestId", reqMap.get("bimRequestId"));
        schema.put("resultCode", "0");
        schema.put("message", "success");
        String mapJson = JSON.toJSONString(schema);
        mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
        return mapJson;
    }

    @RequestMapping("QueryAllUserIdsService")
    public String queryAllUserIds(HttpServletRequest request) {
        Map<String, Object> checkResult = checkRequest(request);
        boolean checkPass = (boolean)checkResult.get("checkPass");
        if(!checkPass){
            return Objects.toString(checkResult.get("errorMsgStr"));
        }

        User user = new User();
        List<User> userList = userService.selectUserList(user);
        List uids = new ArrayList();
        for (int i = 0; i < userList.size(); i++) {
            uids.add(userList.get(i).getUserId());
        }
        Map<String, Object> schema = new HashMap<String, Object>();
        schema.put("resultCode", "0");
        schema.put("userIdList", uids);
        schema.put("message", "success");
        String mapJson = JSON.toJSONString(schema);
        mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
        return mapJson;
    }

    @RequestMapping("QueryUserByIdService")
    public String queryUserById(HttpServletRequest request) {
        Map<String, Object> checkResult = checkRequest(request);
        boolean checkPass = (boolean)checkResult.get("checkPass");
        if(!checkPass){
            return Objects.toString(checkResult.get("errorMsgStr"));
        }

        Map<String, Object> reqMap = (Map<String, Object>)checkResult.get("reqMap");

        String userId = Objects.toString(reqMap.get("bimUid"));
        UserForBim userForBim = new UserForBim();
        User user = userService.selectUserById(Long.parseLong(userId));
        BeanUtils.copyProperties(user,userForBim);
        List<Role> roles = user.getRoles();
        List<Post> posts = postService.selectPostsByUserId(user.getUserId());
        List<String> roleIds = new ArrayList<>();
        List<String> postIds = new ArrayList<>();
        if (roles != null && roles.size() > 0) {
            for (int i = 0; i < roles.size(); i++) {
                roleIds.add(String.valueOf(roles.get(i).getRoleId()));
            }
        }
        if (posts != null && posts.size() > 0) {
            for (int i = 0; i < posts.size(); i++) {
                postIds.add(String.valueOf(posts.get(i).getPostId()));
            }
        }
        userForBim.setRoleIds(roleIds.toString());
        userForBim.setPostIds(postIds.toString());
        Map<String, Object> schema = new HashMap<String, Object>();
        schema.put("bimRequestId", reqMap.get("bimRequestId"));
        schema.put("resultCode", "0");
        schema.put("account", userForBim);
        schema.put("message", "success");
        String mapJson = JSON.toJSONString(schema);
        mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
        return mapJson;
    }

    @RequestMapping("OrgCreateService")
    public String orgCreate(HttpServletRequest request) {
        Map<String, Object> checkResult = checkRequest(request);
        boolean checkPass = (boolean)checkResult.get("checkPass");
        if(!checkPass){
            return Objects.toString(checkResult.get("errorMsgStr"));
        }

        Map<String, Object> reqMap = (Map<String, Object>)checkResult.get("reqMap");
        String s = JSON.toJSONString(reqMap);
        Dept dept = JSONObject.parseObject(s, Dept.class);
        deptService.insertDept(dept);
        final Long uid = dept.getDeptId();
        Map<String, Object> schema = new HashMap<String, Object>();
        schema.put("uid", uid);
        schema.put("bimRequestId", reqMap.get("bimRequestId"));
        schema.put("resultCode", "0");
        schema.put("message", "success");
        String mapJson = JSON.toJSONString(schema);
        mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
        return mapJson;
    }

    @RequestMapping("OrgDeleteService")
    public String orgDelete(HttpServletRequest request) {
        Map<String, Object> checkResult = checkRequest(request);
        boolean checkPass = (boolean)checkResult.get("checkPass");
        if(!checkPass){
            return Objects.toString(checkResult.get("errorMsgStr"));
        }

        Map<String, Object> reqMap = (Map<String, Object>)checkResult.get("reqMap");
        deptService.deleteDeptByIdReal(Long.parseLong(Objects.toString(reqMap.get("bimOrgId"), "")));
        Map<String, Object> schema = new HashMap<String, Object>();
        schema.put("bimRequestId", reqMap.get("bimRequestId"));
        schema.put("resultCode", "0");
        schema.put("message", "success");
        String mapJson = JSON.toJSONString(schema);
        mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
        return mapJson;
    }

    @RequestMapping("OrgUpdateService")
    public String orgUpdate(HttpServletRequest request) {
        Map<String, Object> checkResult = checkRequest(request);
        boolean checkPass = (boolean)checkResult.get("checkPass");
        if(!checkPass){
            return Objects.toString(checkResult.get("errorMsgStr"));
        }

        Map<String, Object> reqMap = (Map<String, Object>)checkResult.get("reqMap");
        String s = JSON.toJSONString(reqMap);
        Long orgId = Long.parseLong(Objects.toString(reqMap.get("bimOrgId")));
        Dept dept = JSONObject.parseObject(s, Dept.class);
        dept.setDeptId(orgId);
        deptService.updateDept(dept);
        Map<String, Object> schema = new HashMap<String, Object>();
        schema.put("bimRequestId", reqMap.get("bimRequestId"));
        schema.put("resultCode", "0");
        schema.put("message", "success");
        String mapJson = JSON.toJSONString(schema);
        mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
        return mapJson;
    }

    @RequestMapping("RoleCreateService")
    public String roleCreate(HttpServletRequest request) {
        Map<String, Object> checkResult = checkRequest(request);
        boolean checkPass = (boolean)checkResult.get("checkPass");
        if(!checkPass){
            return Objects.toString(checkResult.get("errorMsgStr"));
        }

        Map<String, Object> reqMap = (Map<String, Object>)checkResult.get("reqMap");
        String s = JSON.toJSONString(reqMap);
        Role role = JSONObject.parseObject(s, Role.class);
        role.setStatus("0");
        roleService.insertRole(role);
        final Long uid = role.getRoleId();
        Map<String, Object> schema = new HashMap<String, Object>();
        schema.put("uid", uid);
        schema.put("bimRequestId", reqMap.get("bimRequestId"));
        schema.put("resultCode", "0");
        schema.put("message", "success");
        String mapJson = JSON.toJSONString(schema);
        mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
        return mapJson;
    }

    @RequestMapping("RoleDeleteService")
    public String roleDelete(HttpServletRequest request) {
        Map<String, Object> checkResult = checkRequest(request);
        boolean checkPass = (boolean)checkResult.get("checkPass");
        if(!checkPass){
            return Objects.toString(checkResult.get("errorMsgStr"));
        }

        Map<String, Object> reqMap = (Map<String, Object>)checkResult.get("reqMap");
        roleService.deleteRoleById(Long.parseLong(Objects.toString(reqMap.get("bimRid"), "")));
        Map<String, Object> schema = new HashMap<String, Object>();
        schema.put("bimRequestId", reqMap.get("bimRequestId"));
        schema.put("resultCode", "0");
        schema.put("message", "success");
        String mapJson = JSON.toJSONString(schema);
        mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
        return mapJson;
    }

    @RequestMapping("RoleUpdateService")
    public String roleUpdate(HttpServletRequest request) {
        Map<String, Object> checkResult = checkRequest(request);
        boolean checkPass = (boolean)checkResult.get("checkPass");
        if(!checkPass){
            return Objects.toString(checkResult.get("errorMsgStr"));
        }

        Map<String, Object> reqMap = (Map<String, Object>)checkResult.get("reqMap");
        String s = JSON.toJSONString(reqMap);
        Long roleId = Long.parseLong(Objects.toString(reqMap.get("bimRid")));
        Role role = JSONObject.parseObject(s, Role.class);
        role.setRoleId(roleId);
        roleService.updateRole(role);
        Map<String, Object> schema = new HashMap<String, Object>();
        schema.put("bimRequestId", reqMap.get("bimRequestId"));
        schema.put("resultCode", "0");
        schema.put("message", "success");
        String mapJson = JSON.toJSONString(schema);
        mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
        return mapJson;
    }

    @RequestMapping("QueryAllRoleIdsService")
    public String queryAllRoleIds(HttpServletRequest request) {
        Map<String, Object> checkResult = checkRequest(request);
        boolean checkPass = (boolean)checkResult.get("checkPass");
        if(!checkPass){
            return Objects.toString(checkResult.get("errorMsgStr"));
        }

        List<Role> roles = roleService.selectRoleAll();
        List<Long> uids = new ArrayList<>();
        for (int i = 0; i < roles.size(); i++) {
            uids.add(roles.get(i).getRoleId());
        }
        Map<String, Object> schema = new HashMap<String, Object>();
        schema.put("resultCode", "0");
        schema.put("roleIdList", uids);
        schema.put("message", "success");
        String mapJson = JSON.toJSONString(schema);
        mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
        return mapJson;
    }

    @RequestMapping("QueryRoleByIdService")
    public String queryRoleById(HttpServletRequest request) {
        Map<String, Object> checkResult = checkRequest(request);
        boolean checkPass = (boolean)checkResult.get("checkPass");
        if(!checkPass){
            return Objects.toString(checkResult.get("errorMsgStr"));
        }

        Map<String, Object> reqMap = (Map<String, Object>)checkResult.get("reqMap");
        String roleId = Objects.toString(reqMap.get("bimRid"));
        Role role = roleService.selectRoleById(Long.parseLong(roleId));
        Map<String, Object> schema = new HashMap<String, Object>();
        schema.put("bimRequestId", reqMap.get("bimRequestId"));
        schema.put("resultCode", "0");
        schema.put("role", role);
        schema.put("message", "success");
        String mapJson = JSON.toJSONString(schema);
        mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
        return mapJson;
    }

    @RequestMapping("QueryAllOrgIdsService")
    public String queryAllOrgIds(HttpServletRequest request) {
        Map<String, Object> checkResult = checkRequest(request);
        boolean checkPass = (boolean)checkResult.get("checkPass");
        if(!checkPass){
            return Objects.toString(checkResult.get("errorMsgStr"));
        }

        Dept dept = new Dept();
        List<Dept> depts = deptService.selectDeptList(dept);
        List<String> uids = new ArrayList<>();
        for (int i = 0; i < depts.size(); i++) {
            uids.add(String.valueOf(depts.get(i).getDeptId()));
        }
        Map<String, Object> schema = new HashMap<>();
        schema.put("resultCode", "0");
        schema.put("orgIdList", uids);
        schema.put("message", "success");
        String mapJson = JSON.toJSONString(schema);
        mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
        return mapJson;
    }

    @RequestMapping("QueryOrgByIdService")
    public String queryOrgById(HttpServletRequest request) {
        Map<String, Object> checkResult = checkRequest(request);
        boolean checkPass = (boolean)checkResult.get("checkPass");
        if(!checkPass){
            return Objects.toString(checkResult.get("errorMsgStr"));
        }

        Map<String, Object> reqMap = (Map<String, Object>)checkResult.get("reqMap");
        String orgId = Objects.toString(reqMap.get("bimUid"));
        Dept dept = deptService.selectDeptById(Long.parseLong(orgId));
        Map<String, Object> schema = new HashMap<String, Object>();
        schema.put("bimRequestId", reqMap.get("bimRequestId"));
        schema.put("resultCode", "0");
        schema.put("organization", dept);
        schema.put("message", "success");
        String mapJson = JSON.toJSONString(schema);
        mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
        return mapJson;
    }

    @RequestMapping("QueryAllResourceIdsService")
    public String queryAllResourceIdsService(HttpServletRequest request) {
        Map<String, Object> checkResult = checkRequest(request);
        boolean checkPass = (boolean)checkResult.get("checkPass");
        if(!checkPass){
            return Objects.toString(checkResult.get("errorMsgStr"));
        }
        Map<String, Object> reqMap = (Map<String, Object>)checkResult.get("reqMap");
        String resourceType = Objects.toString(reqMap.get("resourceType"));
        if ("post".equals(resourceType)) {
            List<Post> allPositions = postService.selectPostAll();
            List<Long> uids = new ArrayList<>();
            for (int i = 0; i < allPositions.size(); i++) {
                uids.add(allPositions.get(i).getPostId());
            }
            Map<String, Object> schema = new HashMap<String, Object>();
            schema.put("resultCode", "0");
            schema.put("resourceIdList", uids);
            schema.put("message", "success");
            schema.put("bimRequestId", reqMap.get("bimRequestId"));
            String mapJson = JSON.toJSONString(schema);
            mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
            return mapJson;
        }
        return null;
    }

    @RequestMapping("QueryResourceByIdService")
    public String queryResourceByIdService(HttpServletRequest request) {
        Map<String, Object> checkResult = checkRequest(request);
        boolean checkPass = (boolean)checkResult.get("checkPass");
        if(!checkPass){
            return Objects.toString(checkResult.get("errorMsgStr"));
        }
        Map<String, Object> reqMap = (Map<String, Object>)checkResult.get("reqMap");
        String resourceType = Objects.toString(reqMap.get("resourceType"));
        String resourceId = Objects.toString(reqMap.get("resourceId"));
        if ("post".equals(resourceType)) {
            List<Post> allPositions = postService.selectPostAll();
            Post position = null;
            for (int i = 0; i < allPositions.size(); i++) {
                if(allPositions.get(i).getPostId() == Long.valueOf(resourceId)){
                    position = allPositions.get(i);
                    break;
                }
            }
            Map<String, Object> schema = new HashMap<String, Object>();
            schema.put("resultCode", "0");
            schema.put("resource", position);
            schema.put("message", "success");
            schema.put("bimRequestId", reqMap.get("bimRequestId"));
            String mapJson = JSON.toJSONString(schema);
            mapJson = BamboocloudFacade.encrypt(mapJson, "123456", "AES");
            return mapJson;
        }
        return null;
    }
}
